import { defineStore } from "pinia";
import { ref, type Ref } from "vue";

interface IHistory {
  storyList: string[];
}

export default defineStore(
  "history",
  () => {
    const story = JSON.parse(localStorage.getItem("history") || '{"storyList": []}') as IHistory;
    const storyList = ref(story.storyList);
    const addHistoryItem = (item: string) => {
      if (!storyList.value.includes(item)) {
        storyList.value.unshift(item);
      }
    };

    const clearHistory = () => {
      storyList.value = [];
    };

    return {
      storyList,
      addHistoryItem,
      clearHistory,
    };
  },
  {
    persist: true,
  }
);
